package m1_simple;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer {
    public static void main(String[] args)throws Exception {
        // 1.连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("www.wht6.cn");
        f.setPort(5672);
        f.setUsername("/江哥捏脚");
        f.setPassword("admin");
        f.setVirtualHost("/user");
        Connection con = f.newConnection();
        Channel c = con.createChannel();


        // 2.定义队列 helloworld
        c.queueDeclareNoWait(
                "helloworld",
                false,
                false,
                false,
                null);

        //创建回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] a = message.getBody();
                String msg = new String(a);
                System.out.println("收到:"+msg);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
            }
        };

        // 3.消费,接收消息
        c.basicConsume(
                "helloworld",
                true,
                deliverCallback,//处理消息的回调对象
                cancelCallback//取消接收对象的回调对象
                );
    }
}
